import Vue from 'vue'
import axios from 'axios'
import store from '@/store'
import defaultConfig from '@/utils/config'


 
const service = axios.create({
  baseURL: process.env.NODE_ENV === 'development' ? '/apiDev' : defaultConfig.apiBaseUrl, 
  timeout: 60000, // 请求超时时间
})

const err = (error) => {
  if (error.response) {
    let {data,message} = error.response
    Vue.prototype['$message'].error(data || message)
  }else{
    Vue.prototype['$message'].error(error)
  }

  return Promise.reject(error)
};

// request interceptor
service.interceptors.request.use(config => {
  if (config.method === "get") {
      config.data = { unused: 0 }; 
  }

  let token = process.env.NODE_ENV === 'production'  ? sessionStorage.getItem('tokenIframe') || 'aGNieDhzd202V0xHaW1raHNMbktGK2xFQzJqeWRpT0h0QlZQaVdYRXRpQ2lTc1hLZ1ZWVzA1UllzL0FFSVN0RnQ4WENFRlU0OEdaLwp6MlJBSmlwRzFRN21rdU4vejJoSmdiRUt0MUJDNVlLU05tNFo0djdneVIrL1R0Vk05Nm5XSmx0dXJzSVNOU3FvZk9sZVV0c2pSb1JmCmg1eWkxVUkzY3BERzJFSmQwTWJLVmh5QndveENMeHIyazgxQzUzTlFPYjVlQU9lUit5MTladWhQL1JHQ1Nkd2p0VVZNeUtkcTM3ZXEKYWRZamNTcm1sSnd6blFsU2V4cVlwMVZ1S0swYk1mcHp6TXdxZmhkQTgyWGh3cStQbUE9PQ_' : sessionStorage.getItem('tokenIframe') || 'aGNieDhzd202V0xHaW1raHNMbktGK2xFQzJqeWRpT0h0QlZQaVdYRXRpQ2lTc1hLZ1ZWVzA1UllzL0FFSVN0RnQ4WENFRlU0OEdaLwp6MlJBSmlwRzFRN21rdU4vejJoSmdiRUt0MUJDNVlLU05tNFo0djdneVIrL1R0Vk05Nm5XSmx0dXJzSVNOU3FvZk9sZVV0c2pSb1JmCmg1eWkxVUkzY3BERzJFSmQwTWJLVmh5QndveENMeHIyazgxQzUzTlFPYjVlQU9lUit5MTladWhQL1JHQ1Nkd2p0VVZNeUtkcTM3ZXEKYWRZamNTcm1sSnd6blFsU2V4cVlwMVZ1S0swYk1mcHp6TXdxZmhkQTgyWGh3cStQbUE9PQ_'


  config.headers[ 'Authorization' ] = token
  if(config.method == 'get'){
    if(config.params){
      config.params['newDate' + new Date().getTime()] = new Date().getTime()
    }else{
    }

  }
  return config
},(error) => {
  return Promise.reject(error)
})

// response interceptor
service.interceptors.response.use((response) => {
  if(response.config.responseType == "blob"){
    return response
  }
  let {code ,data,message} = response.data;
  if(code != 200){
    if(code === '7012'){
      return response.data
    }else if(code === '7200'){
      Vue.prototype.$errorRef.open({
        isShow:true,
        contentArray: data
      })
    }else{
      Vue.prototype['$message']({
        message: (message || data),
        type: 'error',
        duration: 5000
      })
    }
    
  }

    return response.data
  }, err)



export {
  // installer as VueAxios,
  service as axios,
}